<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
header('Content-type: text/JavaScript');
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');

include_once("file.common.php");

// Input
$file = req('file', '');
$path = req('path', '');
$key = req('key');
$callbackName = req('callback');

$result = TRUE;
$ot = $aot = '';
$isFolder = FALSE;
$exists = FALSE;
$msg = 0;

$fullPath = webPath($path, $file);

if ($validCredentials !== TRUE) {
	if ($callback) {
		$ee = '';
		$ee .= "$callbackName({\n";
		$ee .= "result: false,\n";
		$ee .= "msg: \"invalid credentials\",\n";
		$ee .= "end: true\n";
		$ee .= "});";
		echo $ee;
	}
	exit(0);
}

$pi = pathinfo($fullPath);

if (file_exists($fullPath)) {
	$exists = TRUE;
	if (is_dir($fullPath)) {
		$result = FALSE;
		$isFolder = TRUE;
		$msg = 'requested deletion of a folder, not a file';
	} else {
		unlink($fullPath);
		clearstatcache();
		$result = !file_exists($fullPath);
	}
}
else {
	$result = TRUE;
	$msg = 'file does not exist';
}

$ot .= "$callbackName({\n";
$ot .= '"result":' . json_encode($result) . ",\n";
$ot .= '"file":' . json_encode($file) . ",\n";
$ot .= '"path":' . json_encode($path) . ",\n";
$ot .= '"fullPath":' . json_encode($fullPath) . ",\n";
$ot .= '"cwd":' . json_encode(getcwd()) . ",\n";
$ot .= '"root":' . json_encode($_ENV['DOCUMENT_ROOT']) . ",\n";
$ot .= '"msg":' . json_encode($msg) . ",\n";
$ot .= '"key":' . json_encode($key) . ",\n";
$ot .= '"host":' . json_encode($_ENV['SERVER_NAME']) . ",\n";
$ot .= '"exists":' . json_encode($exists) . ",\n";
$ot .= "})";
echo $ot;
?>
